package com.mrobuy.jdvc.service.impl;

import com.gavel.utils.StringUtils;
import com.mrobuy.jdvc.entity.VCExtPropValue;
import com.mrobuy.jdvc.entity.VCItemExtPropValue;
import com.mrobuy.jdvc.entity.VCItemPropValue;
import com.mrobuy.jdvc.entity.VCPropValue;
import com.mrobuy.jdvc.model.CategoryInfo;
import com.mrobuy.jdvc.service.ItemAttrService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import sun.security.util.Cache;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

@Service("itemAttrService")
public class ItemAttrServiceImpl implements ItemAttrService {

    private static Logger LOG = LoggerFactory.getLogger(ItemAttrService.class);

    private Map<String, Pattern> attrNameMap = new ConcurrentHashMap<>();

    private static Cache<String, List<VCPropValue>> propCache = Cache.newHardMemoryCache(0,3600);

    private static Cache<String, List<VCExtPropValue>> extPropCache = Cache.newHardMemoryCache(0,3600);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public ItemAttrServiceImpl() {
        attrNameMap.put("重量", Pattern.compile("^[(重量)|(净重)]"));
        attrNameMap.put("长度", Pattern.compile("^[(长度)|(全长)]"));
    }

    @Override
    public String getAttrValue(String value, Map<String, String> attrs) {
        if (StringUtils.isBlank(value)
                || attrs == null || attrs.size()==0 ) {
            return null;
        }

        for (String key : attrs.keySet()) {
            if ( StringUtils.trim(key).equalsIgnoreCase(StringUtils.trim(value)) ) {
                return attrs.get(key);
            }
        }

        for (String key : attrs.keySet()) {
            if ( attrNameMap.containsKey(value) ) {
                if ( attrNameMap.get(value).matcher(key).find() ) {
                    return attrs.get(key);
                }
            }
        }

        if ( "长度".equalsIgnoreCase(value) ) {
            return getLength(attrs);
        }

        if ( "宽度".equalsIgnoreCase(value) ) {
            return getWidth(attrs);
        }

        if ( "高度".equalsIgnoreCase(value) ) {
            return getHeight(attrs);
        }

        return null;
    }

    @Override
    public String getAttrValue(String value, Map<String, String> attrs, List<VCItemPropValue> propValues, List<VCItemExtPropValue> extPropValues) {

        if ( StringUtils.isBlank(value) ) {
            return "";
        }

        if ( propValues!=null && propValues.size() > 0 ) {
            for (VCItemPropValue propValue : propValues) {
                if ( value.equalsIgnoreCase(String.valueOf(propValue.getPropname())) ) {
                   return getPropValue(propValue);
                }
            }
        }

        if ( extPropValues!=null && extPropValues.size() > 0 ) {
            for (VCItemExtPropValue extPropValue : extPropValues) {
                if ( value.equalsIgnoreCase(String.valueOf(extPropValue.getPropname())) ) {
                    return getExtPropValue(extPropValue);
                }
            }
        }

        return getAttrValue(value, attrs);
    }


    private String getPropValue(VCItemPropValue propValue){
        List<VCPropValue> list = null;
        switch ( propValue.getType() ) {
            case "1":
                list = propOptions(String.valueOf(propValue.getPropid()));
                for (VCPropValue vcPropValue : list) {
                    if ( String.valueOf(vcPropValue.getValueId()).equalsIgnoreCase(propValue.getPropvid()) ) {
                        return vcPropValue.getValueName();
                    }
                }
                return propValue.getPropvid();
            case "2":
                list = propOptions(String.valueOf(propValue.getPropid()));
                for (VCPropValue vcPropValue : list) {
                    if ( String.valueOf(vcPropValue.getValueId()).equalsIgnoreCase(propValue.getPropvalues()) ) {
                        return vcPropValue.getValueName();
                    }
                }
                return propValue.getPropvalues();
            default:
        }

        return propValue.getPropremark();
    }

    private List<VCPropValue> propOptions(String propid){
        List<VCPropValue> options = propCache.get(propid);
        if ( options==null || options.size()==0 ) {
            options = jdbcTemplate.query("select  * from VC_PROP_VALUE where PROPID = ? ", new BeanPropertyRowMapper(VCPropValue.class), propid);
            if ( options!=null ) {
                propCache.put(propid, options);
            }
        }
        return (options==null ? Collections.EMPTY_LIST : options);
    }



    private String getExtPropValue(VCItemExtPropValue extPropValue){


        switch ( extPropValue.getType() ) {
            case "1":
            case "2":

                List<VCExtPropValue> list = extPropOptions(String.valueOf(extPropValue.getPropid()));
                for (VCExtPropValue vcPropValue : list) {
                    if ( String.valueOf(vcPropValue.getValueId()).equalsIgnoreCase(extPropValue.getExtvalue()) ) {
                        return vcPropValue.getValueName();
                    }
                }
                return extPropValue.getExtvalue();
            default:
        }

        return extPropValue.getExtremark();

    }

    private List<VCExtPropValue> extPropOptions(String propid){
        List<VCExtPropValue> options = extPropCache.get(propid);
        if ( options==null || options.size()==0 ) {
            options = jdbcTemplate.query("select  * from VC_EXTPROP_VALUE where PROPID = ? ", new BeanPropertyRowMapper(VCExtPropValue.class), propid);
            if ( options!=null ) {
                extPropCache.put(propid, options);
            }
        }
        return (options==null ? Collections.EMPTY_LIST : options);
    }


    @Override
    public List<VCItemPropValue> loadPropValues(String code) {
        try {
            return jdbcTemplate.query("select * from VC_ITEM_PROP_VALUE where PRODUCTID = ? ", new BeanPropertyRowMapper<>(VCItemPropValue.class), code);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e.getCause());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public List<VCItemExtPropValue> loadExtPropValues(String code) {
        try {
            return jdbcTemplate.query("select * from VC_ITEM_EXT_PROP_VALUE where PRODUCTID = ? ", new BeanPropertyRowMapper<>(VCItemExtPropValue.class), code);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e.getCause());
        }
        return Collections.EMPTY_LIST;
    }

    @Override
    public String getPropVid(String propid, Object text) {

        List<VCPropValue>  vcPropValues = propOptions(propid);
        if ( vcPropValues==null || vcPropValues.size()==0 ) {
            return null;
        }

        if ( text==null || StringUtils.isBlank(text.toString()) ) {
            return null;
        }

        for (VCPropValue vcPropValue : vcPropValues) {
            if ( text.toString().equalsIgnoreCase(vcPropValue.getValueName()) ) {
                Integer vid = vcPropValue.getValueId();
                if ( vid!=null ) {
                    return vid.toString();
                }
            }
        }

        return null;
    }

    @Override
    public String getExtPropValue(String attId, Object propValue) {
        List<VCExtPropValue>  vcExtPropValues = extPropOptions(attId);
        if ( vcExtPropValues==null || vcExtPropValues.size()==0 ) {
            return null;
        }

        if ( propValue==null || StringUtils.isBlank(propValue.toString()) ) {
            return null;
        }

        for (VCExtPropValue extPropValue : vcExtPropValues) {
            if ( propValue.toString().equalsIgnoreCase(extPropValue.getValueName()) ) {
                Integer vid = extPropValue.getValueId();
                if ( vid!=null ) {
                    return vid.toString();
                }
            }
        }

        return null;
    }

    private String getHeight(Map<String,String> attrs) {

        // 外形尺寸(mm)宽×深×高
        // 外形尺寸(mm)长×宽×高: 882×703×170
        // 尺寸(mm)长×宽×高: 424 x 300 x 55mm
        // 外形尺寸(cm)长×宽×高: 152.4×55.9×127
        // 尺寸(cm)长×宽: 14.5×10
        // 外形尺寸(mm)长×宽×高: 323*139*958

        return null;
    }

    private String getWidth(Map<String,String> attrs) {
        return null;
    }

    private String getLength(Map<String, String> attrs) {
        return null;
    }

    public static void main(String[] args) {

        System.out.println(Math.abs("其他电料辅件".hashCode()));

    }
}
